<template>
  <div class="dialog-container">
    <el-dialog
      :title="dialogType==='add'?'添加模板':dialogType === 'detail'?'模板详情':'修改模板'"
      :visible="addDialogVisible"
      @open="initData"
      @close="closeDialog"
    >
      <el-form
        ref="form"
        :model="dataForm"
        class="dialog-form"
        :rules="rules"
        label-width="110px"
        size="mini"
        :disabled="dialogType === 'detail'"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="表单类型" prop="equipmentTypeId">
              <el-select v-model="dataForm.formType" class="w-100" placeholder="请输入类型">
                <el-option
                  v-for="(item,index) in formTypes"
                  :key="index+'monitor_type'"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表单名称" prop="eqmBrand">
              <el-input v-model="dataForm.name" placeholder="请输入表单名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表单简称" prop="shortName">
              <el-input v-model="dataForm.shortName" placeholder="请输入表单简称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属模板大类" prop="bigType">
              <el-select v-model="dataForm.bigType" class="w-100" placeholder="请输入大类">
                <el-option
                  v-for="(item,index) in bigTypes"
                  :key="index+'monitor_type'"
                  :value="item.code"
                  :label="item.codeName"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模板地址" prop="eqmModel">
              <el-input v-model="dataForm.modelUrl" placeholder="请输入模板地址" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span v-if="dialogType !== 'detail'" slot="footer" class="dialog-footer">
        <el-button :loading="saveLoading" class="sure-btn" size="mini" type="primary" @click="handleSave">保存</el-button>
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { getDict } from '@/utils/dict'
import { add, edit } from '@/api/business/template'

export default {
  name: 'AddEquipmentDialog',
  components: { },
  props: {
    addDialogVisible: {
      type: Boolean,
      default: false
    },
    dialogType: {
      type: String,
      default: 'detail'
    },
    obj: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      saveLoading: false,
      dataForm: {
        name: null,
        modelUrl: null,
        bigType: null,
        shortName: null,
        formType: null
      },
      formTypes: [],
      bigTypes: [],
      rules: {
        // examineResult: [{ required: true, message: '请选择违法行为判定', trigger: 'change' }]
      }
    }
  },
  created() {
    this.formTypes = getDict('e051')
    this.bigTypes = getDict('e058')
  },
  methods: {
    initData() {
      this.saveLoading = false
      if (this.obj) {
        this.dataForm = JSON.parse(JSON.stringify(this.obj))
      }
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    handleSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.saveLoading = true
          if (this.dataForm.id) {
            edit(this.dataForm).then(() => {
              this.$message({
                type: 'success',
                message: '修改成功'
              })
              this.$emit('flushPage')
              this.$emit('closeDialog')
              this.clearData()
              this.saveLoading = false
            }).catch(() => {
              this.saveLoading = false
            })
          } else {
            add(this.dataForm).then(() => {
              this.$message.success('添加成功')
              this.$emit('flushPage')
              this.$emit('closeDialog')
              this.clearData()
              this.saveLoading = false
            }).catch(() => {
              this.saveLoading = false
            })
          }
        }
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.clearData()
      this.$refs['form'].clearValidate()
    },
    clearData() {
      this.dataForm = {
        name: null,
        modelUrl: null,
        bigType: null,
        shortName: null,
        formType: null
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
